<script setup lang="ts">
// 推广状态
import PauseCircleOutlined from "@ant-design/icons-vue/PauseCircleOutlined";
import PlayCircleOutlined from "@ant-design/icons-vue/PlayCircleOutlined";
import MinusCircleOutlined from "@ant-design/icons-vue/MinusCircleOutlined";
import {message} from "ant-design-vue";
import {WxtBizCode, WxtUtils} from "~/contentScripts/views/wxt/lib/wxtUtils";

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  type: {
    type: String,
    default: 'plan',
  },
  rows: {
    type: Array,
    default: () => []
  },
  bizCode: {
    type: String as PropType<WxtBizCode>,
    default: WxtBizCode.关键字推广,
  },
});
const emit = defineEmits([
  "refresh",
]);
const changeStatus = (e: any) => {
  if (props.rows.length===0){
    message.error("请选择需要批量处理的数据！");
    return;
  }
  const displayStatus = e.key;
  if (props.type === 'plan') {
    const campaignIds: string[] = props.rows.map((r: any) => r.campaignId);
    WxtUtils.campaignUpdatePart([
      ...campaignIds.map((campaignId: string) => ({
        campaignId,
        displayStatus,
      })),
    ], props.bizCode).then(res => {
      // visible.value = false;
      if (res.data.info.ok !== true) {
        message.error(res.data.info.message)
        return;
      }
      message.success("操作成功！");
    }).finally(() => {
      emit('refresh');
    });
    return;
  }else if (props.type === 'unit') {  // 单元
    const adgroupIds: string[] = props.rows.map((r: any) => r.adgroupId);
    const campaignIds: string[] = props.rows.map((r: any) => r.campaignId);
    WxtUtils.campaignUpdatePart(undefined,props.bizCode,{
      adgroupList:[...adgroupIds.map((adgroupId: string,index:number) => ({
        campaignId:campaignIds[index],
        adgroupId,
        displayStatus,
      }))],
    },'adgroup').then(res => {
      // visible.value = false;
      if (res.data.info.ok !== true) {
        message.error(res.data.info.message)
        return;
      }
      message.success("操作成功！");
    }).finally(() => {
      emit('refresh');
    });
  }
}
</script>

<template>
  <a-dropdown trigger="click">
    <template #overlay>
      <a-menu @click="changeStatus">
        <a-menu-item key="start">
          <PlayCircleOutlined :style="{
      color: '#1677ff'
    }"
          />
          正在推广
        </a-menu-item>
        <a-menu-item key="pause">
          <PauseCircleOutlined :style="{
      color: '#ff4d4f'
    }"
          />
          暂停推广
        </a-menu-item>
        <a-menu-item key="terminate" v-if="[WxtBizCode.精准人群推广,WxtBizCode['消费者运营-粉丝快'],WxtBizCode['消费者运营-拉新快'],WxtBizCode['消费者运营-获客易'],WxtBizCode['消费者运营-会员快']].includes(bizCode) && type==='plan'">
          <MinusCircleOutlined :style="{
      color: '#444'
    }"
          />
          结束推广
        </a-menu-item>
      </a-menu>
    </template>
    <slot>
      <a-button size="small" type="primary" ghost @click="visible=true">推广状态</a-button>
    </slot>
  </a-dropdown>
</template>

<style scoped>

</style>
